package cn.dubito.miaosha.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liliang
 * @description:
 * @date 2018/4/10.
 */
@Configuration
public class MqConfig {

    public static final String QUEUE = "queue";
    public static final String SECKILL_QUEUE = "seckill_queue";
    public static final String HEAD_QUEUE = "head_queue";
    public static final String TOPIC_QUEUE1 = "topic_queue1";
    public static final String TOPIC_QUEUE2 = "topic_queue2";
    public static final String TOPIC_EXCHANGE = "topic_exchange";
    public static final String FANOUT_EXCHANGE = "fanout_exchange";
    public static final String HEAD_EXCHANGE = "head_exchange";


    /**
     * direct Exchange 交换机模式
     * @return
     */
    @Bean
    public Queue queue() {
        return new Queue(QUEUE,true);
    }

    @Bean
    public Queue SeckillQueue() {
        return new Queue(SECKILL_QUEUE,true);
    }

    /**
     * topic Exchange 交换机模式
     * @return
     */
    @Bean
    public Queue topicQueue1() {
        return new Queue(TOPIC_QUEUE1,true);
    }

    @Bean
    public Queue topicQueue2() {
        return new Queue(TOPIC_QUEUE2,true);
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE);
    }

    @Bean
    public Binding topicBind1() {
        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("topic.key1");
    }

    @Bean
    public Binding topicBind2() {
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("topic.#");
    }

    /**
     * fanout exchange 广播交换机模式
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange() {
         return new FanoutExchange(FANOUT_EXCHANGE);
    }

    @Bean
    public Binding fanoutBind1() {
        return BindingBuilder.bind(topicQueue1()).to(fanoutExchange());
    }

    @Bean
    public Binding fanoutBind2() {
        return BindingBuilder.bind(topicQueue2()).to(fanoutExchange());
    }

    /**
     *
     * Header Exchange 交换机模式
     */
    @Bean
    public HeadersExchange headersExchange(){
         return new HeadersExchange(HEAD_EXCHANGE);
    }

    @Bean
    public Queue headQueue() {
        return new Queue(HEAD_QUEUE,true);
    }

    @Bean
    public Binding headBind() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("head1","value1");
        map.put("head2","value2");
        return BindingBuilder.bind(headQueue()).to(headersExchange()).whereAll(map).match();
    }
}
